package com.qyhl.cloud.webtv.module_integral.wallet.cash;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.TextAppearanceSpan;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.cloud.webtv.module_integral.R;
import com.qyhl.cloud.webtv.module_integral.R2;
import com.qyhl.cloud.webtv.module_integral.common.IntergralUrl;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.SpanUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.constant.PathConfigConstant;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

/**
 * 绑定支付宝
 */
@Route(path = ARouterPathConstant.BIND_ALIPAY)
public class AlipayBindActivity extends BaseActivity {
    @BindView(R2.id.alipay_title)
    TextView alipayTitle;
    @BindView(R2.id.alipay_toolbar)
    Toolbar alipayToolbar;
    @BindView(R2.id.alipay_account_et)
    EditText alipayAccountEt;
    @BindView(R2.id.alipay_name_et)
    EditText alipayNameEt;
    @BindView(R2.id.root_layout)
    ConstraintLayout rootLayout;
    @BindView(R2.id.alipay_check_way)
    TextView alipayCheckWay;

    @Autowired(name = "account")
     String alipayAccount;
    @Autowired(name = "name")
     String alipayName;

    private static final int RESULT_CODE = 200;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentView() {
        return R.layout.inter_activity_bind_alipay;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        initBind();
    }

    @Override
    protected void setListener() {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (android.R.id.home == item.getItemId()) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initBind() {
        alipayTitle.setText("绑定支付宝账号");
        setSupportActionBar(alipayToolbar);


        alipayAccountEt.setText(alipayAccount);
        alipayNameEt.setText(alipayName);

        SpanUtils spanUtils = new SpanUtils(this);
        spanUtils.appendImage(R.drawable.finger_tip)
                .append("支付宝实名与账号核对方法")
                .setSpans(new TextAppearanceSpan(this,R.style.inter_alipay_tag))
                .setUnderline()
                .setClickSpan(new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        Bundle bundle = new Bundle();
                        bundle.putString("url","http://wxshare.i2863.com/payPage/payIndex.html");
                        bundle.putString("title","支付宝实名与账号核对方法");
                        bundle.putBoolean("hasShare", false);
                        RouterManager.router(ARouterPathConstant.ADV,bundle);
                    }
                });
        alipayCheckWay.setText(spanUtils.create());
        alipayCheckWay.setMovementMethod(LinkMovementMethod.getInstance());
    }


    @OnClick(R2.id.alipay_commit_btn)
    public void onViewClicked() {
        alipayAccount = alipayAccountEt.getText().toString();
        if (!StringUtils.isMobileNO(alipayAccount) && !StringUtils.isEmail(alipayAccount)) {
            Toasty.normal(this, "这不是一个正确的支付宝账户！").show();
            return;
        }
        alipayName = alipayNameEt.getText().toString();
        if (StringUtils.isEmpty(alipayName)) {
            Toasty.normal(this, "请填写姓名！").show();
            return;
        } else if (!StringUtils.isOffcialName(alipayName)) {
            Toasty.normal(this, "姓名中包含非法字符！").show();
            return;
        }


        showSimpleLoading();
        Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
        JSONObject object = new JSONObject();
        String userName = CommonUtils.getInstance().getUserName();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.version);
            object.put("name", "bindAlipay");
            object.put("username", userName);
            object.put("alipayAccount", alipayAccount);
            object.put("alipayName", alipayName);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        dismissLoading();
                        Toasty.normal(AlipayBindActivity.this, e.getMsg()).show();
                    }

                    @Override
                    public void onSuccess(String se) {
                        dismissLoading();
                        Toasty.normal(AlipayBindActivity.this, "绑定成功！").show();
                        Intent intent = new Intent();
                        intent.putExtra("account", alipayAccount);
                        intent.putExtra("name", alipayName);
                        setResult(RESULT_CODE, intent);
                        finish();
                    }
                });

    }
}
